#ifndef HEAD_H
#define HEAD_H

#include <my_header.h>
#include <mysql/mysql.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include "workqueue.h"


#define MAX_STACK_SIZE 100
#define RETERROR1() do {char buf[1024] = {0}; strerror_r(errno,buf,sizeof(buf)); TLVResponse(sockfd,buf,IS_NOT_OK);} while(0)
#define RETERROR2(expected) do {char buf[] = expected; TLVResponse(sockfd,buf,IS_NOT_OK);} while(0)
#define RETMYSQLERROR(qret,expected) do{if(qret!=0){char buf[1024] = {0}; sprintf(buf,"%s:%s",expected,mysql_error(mysql)); TLVResponse(sockfd,buf,IS_NOT_OK); return -1;}} while(0)
#define RETOK(expected) do {char buf[] = expected; TLVResponse(sockfd,buf,IS_OK);} while(0)

#define KICK_TIME 30

// 枚举：用于填充火车的type字段
enum{
    CD,
    LS,
    PUTS,
    GETS,
    PWD ,
    MKDIR,
    RMDIR,
    RMDIR_R,
    REMOVE,
    FILE_LEN,
    SEND_FILE,
    CANCEL,
    IS_OK,
    IS_NOT_OK,
    SIGNUP_USERNAME,
    SIGNUP_SALT,
    SIGNUP_CRYPTED,
    LOGIN_USERNAME,
    LOGIN_SALT,
    LOGIN_CRYPTED,
    NEW_CLIENT,
    IPADDR_PORT
};


// 数据结构：线程相关信息
typedef struct threadInfo_s{
    pthread_t threadId;
    
}threadInfo_t;

// 数据结构：线程信息集
typedef struct tidArr_s{
    threadInfo_t *arr;
    int count;
}tidArr_t;

// 数据结构：线程池
typedef struct threadPool_s{
    pthread_mutex_t mutex;
    pthread_cond_t cond;

    workqueue_t workQueue;
    tidArr_t tidArr;
    int efd;
    
    int recordUserId[1024];
}threadPool_t;

typedef struct kickNode_s{
    int netfd;
    struct kickNode_s *pPre;
    struct kickNode_s *pNext;
}kickNode_t;

typedef struct kickList_s{
    kickNode_t *head;
    kickNode_t *tail;
}kickList_t;

typedef struct fdAndPtr_s{
    int netfd;
    kickNode_t *pNode;
}fdAndPtr_t;

// 功能：建立TCP连接
int establishConnect(char *ip, char *port);

// 功能：添加监听                                                                                                                   
int epollAdd(int efd, int monitorfd);

int epollDel(int efd, int delfd);

// 功能：创建线程池
int makeWorker(threadPool_t *threadPool, int threadCount);

// 功能：回复响应
int TLVResponse(int sockfd, const char *buf, int type);

// 功能：文件传输时响应长度信息
int TLVResponseLen(int sockfd, int len);

// 功能：接收请求
int recvRequest(int sockfd, train_t *train);

// 功能：登录验证
int loginVerity(MYSQL *mysql, int sockfd, train_t *mess);

// 功能：注册验证
int signUpVerity(MYSQL *mysql, int sockfd, train_t *mess);

// 功能：处理上传
int dealGets(MYSQL *mysql, int sockfd, train_t *mess, threadPool_t *threadPool);

// 功能：处理下载
int dealPuts(MYSQL *mysql, int sockfd, train_t *mess, threadPool_t *threadPool);

int recvfile(int clientSockfd, int fd, int fd_progress, int curFileSize, int fileSize, char *MD5_progress);

int dealPutsOrder(int clientSockfd, train_t *train);

int dealGetsOrder(int clientSockfd, train_t *train);






int findPath(MYSQL *mysql, int userid, int sockfd, char *destPath, int *destPos);
int divPath(char *parent, char *destFile, char *destPath);
int cdOrder(MYSQL *mysql, int userid, int sockfd, char *destPath);
int lsOrder(MYSQL *mysql, int userid, int sockfd, char *destPath);
int mkdirOrder(MYSQL *mysql, int userid, int sockfd, char *destPath);
int rmdirOrder(MYSQL *mysql, int userid, int sockfd, char *destPath);
int rmdir_r(MYSQL *mysql, int userid, int sockfd, int destPos);
int rmdir_rOrder(MYSQL *mysql, int userid, int sockfd, char *destPath);
int pwdOrder(MYSQL *mysql, int userid, int sockfd);
int removeOrder(MYSQL *mysql, int userid, int sockfd, char *destPath);

int recvOrderTrain(int sockfd, train_t *train);

/* int recvFile(user_t *user, int fd, int offset, int fileSize); */
/* int sendFile(user_t *user, int fd, int offset, int fileSize); */
int ls_l(char *buf, int size, struct dirent *entry);


#endif

